package cn.iotab.iotkit.transport.api.access.model.message;

import cn.iotab.iotkit.transport.api.access.model.custom.ProtocolConstant;
import cn.iotab.iotkit.transport.api.access.model.exception.IncompleteMessageException;
import cn.iotab.iotkit.transport.api.common.util.JsonUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouhao
 * @since 1.0.0
 */
public class CommonDeviceMessage implements DeviceMessage, MessageCodec {

    private String productKey;
    private String deviceId;

    private long timestamp;

    private final OriginMessage message;


    public CommonDeviceMessage() {
        this.message = new OriginMessage();
    }

    @Override
    public void fromOriginMessage(String productKey, String deviceId, OriginMessage message) {
        this.productKey = productKey;
        this.deviceId = deviceId;
        this.message.from(message);
        this.timestamp = message.getTs() == null ? System.currentTimeMillis() : message.getTs();
    }

    @Override
    public OriginMessage toOriginMessage() {
        if (timestamp > 0) {
            message.setTs(timestamp);
        }
        try {
            message.validate();
        } catch (Exception e) {
            throw new IncompleteMessageException("message format error, cause="
                    + e.getMessage() + ", message: " + message);
        }
        return message;
    }


    @Override
    public String getMessageId() {
        return message.getId();
    }

    @Override
    public String getMessageVersion() {
        return message.getVer();
    }

    @Override
    public String getProductKey() {
        return productKey;
    }

    public void setProductKey(String productKey) {
        this.productKey = productKey;
    }

    @Override
    public String getDeviceId() {
        return deviceId;
    }

    @Override
    public String getOriginType() {
        return message.getType();
    }

    @Override
    public boolean needAck() {
        return ProtocolConstant.NEED_ACK.equals(message.getAck());
    }

    @Override
    public long getTimestamp() {
        return this.timestamp;
    }

    @Override
    public JSONObject getJsonParam() {
        return message.getParam();
    }

    public void setMessageId(String messageId) {
        this.message.setId(messageId);
    }

    public void setMessageVersion(String messageVersion) {
        this.message.setVer(messageVersion);
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public void setOriginType(String originType) {
        this.message.setType(originType);
    }

    public void setNeedAck() {
        this.message.setAck(ProtocolConstant.NEED_ACK);
    }
    public void setNoNeedAck(){
        this.message.setAck(ProtocolConstant.NO_NEED_ACK);
    }

    public void setTimestamp(long timestamp) {
        this.message.setTs(timestamp);
    }

    public void setJsonParam(JSONObject jsonParam) {
        this.message.setParam(jsonParam);
    }

    @Override
    public String toString() {
        return JsonUtil.toJsonString(this);
    }
}
